/**
 * 
 */
package com.demo2do.boyle.backend.web.controller.customer.withdrawal;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.boyle.backend.service.customer.WithdrawalService;
import com.demo2do.boyle.backend.web.view.ExcelTemplateView;
import com.demo2do.boyle.common.entity.customer.wallet.Withdrawal;
import com.demo2do.boyle.common.entity.customer.wallet.WithdrawalStatus;
import com.demo2do.boyle.common.entity.system.Principal;
import com.demo2do.core.entity.Result;
import com.demo2do.core.utils.JsonUtils;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;
import com.demo2do.core.web.resolver.Secure;

/**
 * @author Leo
 *
 */
@Controller
@RequestMapping("/customer-withdrawals")
@MenuSetting("menu-customer")
public class WithdrawalController {

	@Autowired
	private WithdrawalService withdrawalService;
	
	/**
     * list all withdrawals
     * 
     * @return the withdrawal list
     */
    @RequestMapping(value= "")
    @MenuSetting("submenu-customer-withdrawal")
    public ModelAndView list(@RequestParam(value = "status",required = false) WithdrawalStatus status ,Page page) {  	
        ModelAndView modelAndView = new ModelAndView("customer/withdrawal/withdrawal-list");
        
        List<Withdrawal> withdrawals = withdrawalService.list(status, page);
        
        modelAndView.addObject("withdrawals", withdrawals);
        modelAndView.addObject("status", status);
        
        return modelAndView;
    }
    
    /**
     * view the withdrawal detail
     * 
     * @param id
     * @return
     */
    @RequestMapping("/{id}")
    @MenuSetting("submenu-customer-withdrawal")
    public ModelAndView view(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView("customer/withdrawal/withdrawal-view");
        
        Withdrawal withdrawal = withdrawalService.load(Withdrawal.class, id);
        
        modelAndView.addObject("withdrawal", withdrawal);
        
        return modelAndView;
    }
    
    /**
     * display the audit panel
     * 
     * @param id
     * @return the view to confirm the submit
     */
    @RequestMapping("/{id}/audit")
    public ModelAndView audit(@PathVariable("id") Integer id) {
    	ModelAndView modelAndView = new ModelAndView("customer/withdrawal/withdrawal-audit");
		modelAndView.addObject("id", id);
		return modelAndView;
    }
    
    /**
     * do audit 
     * 
     * @param id
     * @return the audit result with message
     */
    @RequestMapping(value ="/{id}/audit", method = RequestMethod.PATCH )
    public @ResponseBody String onAudit(@PathVariable("id") Integer id, @Secure Principal principal) {
    	Result result = new Result();
    	try{
    		Withdrawal withdrawal = withdrawalService.load(Withdrawal.class, id);
    		
    		withdrawalService.audit(withdrawal.operator(principal.getName()));
    		
        	result.success().message("审核成功");
    	}catch(Exception e){
    		result.fail().message("审核失败");
    	}
    	return JsonUtils.toJsonString(result);
    }
    
    /**
     * do transfer 
     * 
     * @param id
     * @return the transfer result with message
     */
    @RequestMapping(value ="/transfer", method = RequestMethod.POST )
    public @ResponseBody String onTransfer(@RequestParam("withdrawalIds") Integer[] ids) {
    	Result result = new Result();
    	try{
        	if (ids != null && ids.length > 0) {
        		withdrawalService.batchTransfer(ids);
        		result.success().message("打款成功");
			} else {
				result.success().message("请至少勾选一个提现申请！");
			}
        	
    	}catch(Exception e){
    		result.fail().message("打款失败");
    	}
    	return JsonUtils.toJsonString(result);
    }
    
    /**
     * dipslay the export excel
     * 
     * @param id
     */
    @RequestMapping("/export")
	public ModelAndView export(HttpServletResponse response) {
    	
		ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("template.xls","withdrawals.xls"));

		List<Withdrawal> withdrawals = withdrawalService.list(WithdrawalStatus.TRANSFERING);
		modelAndView.addObject("withdrawals", withdrawals);
		
		return modelAndView;
	}
    
    /**
     * batch update withdrawal status to EXPORTED
     * 
     * @return the export result with message
     */
    @RequestMapping(value = "/export", method = RequestMethod.PATCH )
    public @ResponseBody String onExport() {
    	Result result = new Result();
    	try{
    		withdrawalService.batchExport(WithdrawalStatus.TRANSFERING);
        	result.success().message("操作成功");
    	}catch(Exception e){
    		result.fail().message("操作失败");
    	}
    	return JsonUtils.toJsonString(result);
    }
}
